<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>数据,方法和计算属性和侦听器</title>
  </head>
  <body>
    <script src="https://unpkg.com/vue@next"></script>

    <div id="root"></div>

    <script>
      // data & methods & computed & watcher

      // computed 和 methods 都能实现的功能  建议使用computed  因为有缓存
      // computed 和 watcher 都能实现的功能  建议使用computed  因为更加简洁

      const app = Vue.createApp({
        data() {
          return {
            message: "hello world",
            count: 2,
            price: 5,
            newTotal:10
          };
        },

        watch:{
          price(newVal,oldVal){

            this.newTotal = newVal * this.count;
            
            //console.log(newVal,oldVal);
            //setTimeout(() => {
            //  console.log("price change");
            //},2000)
          }
        },

        
        //当计算属性依赖的内容发生变更的时候  就会重新计算
        computed: {  
          total() {
            //return this.count * this.price;
            return Date.now();
          },
        },

        methods: {
          //handleClick() {
          //  console.log("handleClick");
          //},
          //formatString(string){
          //  return string.toUpperCase();
          //}
          // 只要页面重新渲染 就会重新计算
          //getTotal(){
          //  return Date.now();
          //}
        },

        //template: `<div @click="handleClick">{{formatString(message)}}</div>`,
        template: `<div>{{message}}{{newTotal}}</div>`,
      });

      const vm = app.mount("#root");
    </script>
  </body>
</html>
